########################################################################################################################
########################################################################################################################
# Weber and Klar: Final Code to recode and save CCAP and ANES Data #
# CCAP Dependencies:
# CCAP2016_UARZ_Deliverables//CCAP2016_UARZ_OUTPUT.sav
# Produces:
# sm.data
# ANES Dependencies:
# C2008 ANESTS/anes_timeseries_2008_stata12.dta
# Produces:
# ANES.data.Rdata
########################################################################################################################
rm(list=ls())
require(foreign)
detach("package:dplyr")
require(car)
##Recode to place on 0/1 scale###
zero.one<-function(x){
  min.x<-min(x, na.rm=T)
  max.x<-max(x-min.x, na.rm=T)
  return((x-min.x)/max.x)
}
# Load data
ccap.data<-read.spss("/Users/chrisweber/Desktop/Advances Analysis/Data/CCAP2016_UARZ_Deliverables/CCAP2016_UARZ_OUTPUT.sav",
                     use.value.labels=FALSE, to.data.frame=TRUE)
interest<-recode(ccap.data$campint_w10, "1=3; 2=2; 3=1; else=NA")
news<-recode(ccap.data$newsint_w10, "1=5; 2=4; 3=3; 4=2; 5=1; else=NA")
watch.debate1<-recode(ccap.data$debate1_1_w10, "1=1; 2=0; else=NA") #Watched all the debate
watch.debate.vp<-recode(ccap.data$vp_debate1_1_w10, "1=1; 2=0; else=NA") #Watched all the debate
watch.debate2<-recode(ccap.data$debate2_1_w10, "1=1; 2=0; else=NA") #Watched all the debate
watch.debate3<-recode(ccap.data$debate3_1_w10, "1=1; 2=0; else=NA") #Watched all the debate
trump.tape<-recode(ccap.data$tape_w10, "1=3; 2=2; 3=1; else=NA") #Saw Tape
response.tape<-recode(ccap.data$tape_trump_w10, "1=1; 2=3; 3=2; else=NA") #Tape made things worse
tape.relevance<-recode(ccap.data$tape_relevancy_w10, "1=2; 2=1; else=NA") #Tape is relevant
facebook<-recode(ccap.data$fbookposts_w10, "1=1; 2=3; 3=2; else=NA") #facebook feed, nones are dropped
#### Emotion Items ####
dem.anger<-recode(ccap.data$HRC_angry_w10, "1=5; 2=4; 3=3; 4=2; 5=1; else=NA") #HRC Angry
dem.afraid<-recode(ccap.data$HRC_afraid_w10, "1=5; 2=4; 3=3; 4=2; 5=1; else=NA") #HRC Afraid
dem.worried<-recode(ccap.data$HRC_worried_w10, "1=5; 2=4; 3=3; 4=2; 5=1; else=NA") #HRC Worried
dem.disgusted<-recode(ccap.data$HRC_disgusted_w10, "1=5; 2=4; 3=3; 4=2; 5=1; else=NA") #HRC Disgusted
dem.hopeful<-recode(ccap.data$HRC_hopeful_w10, "1=5; 2=4; 3=3; 4=2; 5=1; else=NA") #HRC Hopeful
dem.excited<-recode(ccap.data$HRC_excited_w10, "1=5; 2=4; 3=3; 4=2; 5=1; else=NA") #HRC Excited
rep.anger<-recode(ccap.data$DT_angry_w10, "1=5; 2=4; 3=3; 4=2; 5=1; else=NA") #DT ANgry
rep.afraid<-recode(ccap.data$DT_afraid_w10, "1=5; 2=4; 3=3; 4=2; 5=1; else=NA") #DT Afraid
rep.worried<-recode(ccap.data$DT_worried_w10, "1=5; 2=4; 3=3; 4=2; 5=1; else=NA") #DT Worried
rep.disgusted<-recode(ccap.data$DT_disgusted_w10, "1=5; 2=4; 3=3; 4=2; 5=1; else=NA") #DT Disgusted
rep.hopeful<-recode(ccap.data$DT_hopeful_w10, "1=5; 2=4; 3=3; 4=2; 5=1; else=NA") #DT Hopeful
rep.excited<-recode(ccap.data$DT_excited_w10, "1=5; 2=4; 3=3; 4=2; 5=1; else=NA") #DT Excited
#### Voting Items
will.vote<-recode(ccap.data$likely_voter16_w10, "1=5; 2=4; 3=3; 4=2; 5=1; 7=5; else=NA") #Probably will vote/have voted.
pres.vote<-recode(ccap.data$presvote16_w10, "1='Hillary'; 2='Trump'; 3:5='Other'; else=NA") #Vote Choice
pres.twoparty<-recode(ccap.data$presvote16_w10, "1=0; 2=1; else=NA") #Vote Choice, Two party, Trump
pres.prediction<-recode(ccap.data$presvote16_win_w10, "1=0; 3=2; 2=3; else=NA") #Vote Prediction. Middle is unsure
### Self Monitoring ####
sm.1<-(recode(ccap.data$UARZ_q1, "8=NA")-1)
sm.2<-(recode(ccap.data$UARZ_q2, "8=NA")-1)
sm.3<-(recode(ccap.data$UARZ_q3, "8=NA")-1)
dem.feel<-recode(ccap.data$UARZ_q17, "998=NA")
rep.feel<-recode(ccap.data$UARZ_q18, "998=NA")
ind.feel<-recode(ccap.data$UARZ_q19, "998=NA")
### Demographics ###
female<-recode(ccap.data$gender, "2=0")
white<-recode(ccap.data$race, "1=1; 2:8=0; else=NA")
non.white<-recode(ccap.data$race, "1=0; 2:8=1; else=NA")
black<-recode(ccap.data$race, "2=1; 1=0; 3:8=0; else=NA")
latino<-recode(ccap.data$race, "3=1; 1:2=0; 4:8=0; else=NA")
other<-recode(ccap.data$race, "4:8=1; 1:3=0; else=NA")
age<-2016-ccap.data$birthyr
education<-recode(ccap.data$educ, "5:6=1; 1:4=0; else=NA")
income<-recode(ccap.data$faminc, "97=NA")
born.again<-recode(ccap.data$pew_bornagain, "2=1; 1=0; else=NA")
rel_import<-recode(ccap.data$pew_religimp, "1=4; 2=3; 3=2; 4=1; else=NA")
## Denomination ###
catholic<-recode(ccap.data$religpew, "2=1; 1=0; 3:12=0; else=NA")
no.religion<-recode(ccap.data$religpew, "9:11=1; 1:8=0; 12=0; else=NA")
other.religion<-recode(ccap.data$religpew, "1:2=0; 3:8=1; 12=1; 9:11=0; else=NA")
#### Ideology and PID ####, all coded in conservative direction
ideology<-(recode(ccap.data$ideo5_w10, "8=NA; 9=NA; 6=NA")-1)/4
pid<-recode(ccap.data$pid7_w10, "8=NA; 99=NA; 98=NA")
vote<-(recode(ccap.data$presvote16_w10, "1=0; 2=1; else=NA")) # Vote Trump
### Social Distance ###
marry.rep<-recode(ccap.data$UARZ_q7, "1=7; 2=6; 3=5; 4=4; 5=3; 6=2; 7=1; else=NA") #Uncomfortable marrying rep
marry.dem<-recode(ccap.data$UARZ_q8, "1=7; 2=6; 3=5; 4=4; 5=3; 6=2; 7=1; else=NA") #Uncomfortable marrying dem
marry.ind<-recode(ccap.data$UARZ_q9, "1=7; 2=6; 3=5; 4=4; 5=3; 6=2; 7=1; else=NA") #Uncomfortable marrying ind
marry.black<-recode(ccap.data$UARZ_q10, "1=7; 2=6; 3=5; 4=4; 5=3; 6=2; 7=1; else=NA") #Uncomfortable marrying black
marry.muslim<-recode(ccap.data$UARZ_q11, "1=7; 2=6; 3=5; 4=4; 5=3; 6=2; 7=1; else=NA") #Uncomfortable marrying muslim
marry.activist<-recode(ccap.data$UARZ_q12, "1=7; 2=6; 3=5; 4=4; 5=3; 6=2; 7=1; else=NA") #Uncomfortable marrying activist
marry.atheist<-recode(ccap.data$UARZ_q13, "1=7; 2=6; 3=5; 4=4; 5=3; 6=2; 7=1; else=NA") #Uncomfortable marrying atheist
### Neighbors
move.neighbor<-recode(ccap.data$UARZ_q14, "1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA") #Important to share views
move.rep<-recode(ccap.data$UARZ_q15, "1=7; 2=6; 3=5; 4=4; 5=3; 6=2; 7=1; else=NA") #Uncomfortable marrying rep
move.dem<-recode(ccap.data$UARZ_q16, "1=7; 2=6; 3=5; 4=4; 5=3; 6=2; 7=1; else=NA") #Uncomfortable marrying rep
## Context, Left-Right
context.ideo<-recode(ccap.data$UARZ_q4, "8=NA") # Mostly conservative
context.pid<-recode(ccap.data$UARZ_q5, "8=NA") # Mostly Republican
context.clinton<-recode(ccap.data$UARZ_q6, "8=NA") #Mostly Trump

##### Data for Paper #####
sm.data<-data.frame(pid, ideology, dem.anger, dem.afraid, dem.worried, dem.disgusted, dem.hopeful, dem.excited,
                       rep.anger, rep.afraid, rep.worried, rep.disgusted, rep.hopeful, rep.excited, 
                       catholic, no.religion, other.religion, female,  education,
                       age, non.white, marry.rep, marry.dem, marry.ind, marry.black,
                       marry.muslim, marry.activist, marry.atheist, move.neighbor, move.rep,
                       move.dem, news, sm.1, sm.2, sm.3, context.ideo, context.pid, context.clinton, 
                       dem.feel, rep.feel, ind.feel, vote, facebook)
sm.data$pid.3<-recode(sm.data$pid, "1='Democrat'; 2='Democrat'; 3='Democrat'; 4='Independent'; 5='Republican'; 6='Republican'; 7='Republican'") #Leaners as partisan
save(sm.data, file= "/Users/chrisweber/Desktop/Advances Analysis/Data/ccap.data.Rdata")
summary(sm.data)


### ANES data ####
###________________________________________________________________________________________###
# Load the ANES data
###________________________________________________________________________________________###
rm(list=ls())
require(foreign)
require(car)
require(psych)
zero.one<-function(x){
  min.x<-min(x, na.rm=T)
  max.x<-max(x-min.x, na.rm=T)
  return((x-min.x)/max.x)
}
data<-read.dta("/Users/chrisweber/desktop/Advances Analysis/Data/2008 ANESTS/anes_timeseries_2008_stata12.dta",
               convert.factors=FALSE, convert.dates=TRUE, warn.missing.labels=FALSE,
               convert.underscore=TRUE)

state.fips<-unlist(strsplit(as.character(data$V081201b), "[^[:digit:]]"))
final.data<-data.frame(as.numeric(state.fips[state.fips!=""]))

##Self monitoring##
sm1<-recode(as.numeric(data$V085307),"1=5; 2=4; 3=3; 4=2; 5=1; else=NA") #Put on a show
sm2<-recode(as.numeric(data$V085308),"1=5; 2=4; 3=3; 4=2; 5=1; else=NA") #Good actor?
sm3<-recode(as.numeric(data$V085309),"1=5; 2=4; 3=3; 4=2; 5=1; else=NA") # Center of attention
alpha(cbind(sm1, sm2, sm3)) #0.66
final.data$self.monitoring<-zero.one(rowMeans(cbind(sm1, sm2, sm3), na.rm=T))
#Ideological polarization
final.data$dem.ideo<-recode(as.numeric(data$V083070a),"1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA") 
final.data$rep.ideo<-recode(as.numeric(data$V083070b),"1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA") 
final.data$dem.ideo2<-recode(as.numeric(data$V083071a),"1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA") 
final.data$rep.ideo2<-recode(as.numeric(data$V083071b),"1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA") 
final.data$difference2<-(dem.ideo-rep.ideo+6)/12
###Religiosity###
bible<-recode(as.numeric(data$V083184), "1=3; 2=2; 3=1; else=NA")
final.data$bible<-zero.one(bible)
final.data$catholic<-recode(as.numeric(data$V083185b), "2=1; 0:1=0; 3:8=0; else=NA")
final.data$protestant<-recode(as.numeric(data$V083185b), "1=1; 0=0; 2:8=0; else=NA")
final.data$no.religion<-recode(as.numeric(data$V083185b), "0=1; 1:8=0; else=NA")
final.data$other.religion<-recode(as.numeric(data$V083185b), "7=1; 0:6=0; 8=0; else=NA")

other.religion<-recode(ccap.data$religpew, "1:2=0; 3:8=1; 12=1; 9:11=0; else=NA")

#Race
final.data$white<-recode(as.numeric(data$V081102),"1=1; 2:7=0; else=NA")
final.data$non.white<-recode(as.numeric(data$V081102),"1=0; 2:7=1; else=NA")
#Demographics
final.data$female<-recode(as.numeric(data$V081101),"2=1; 1=0; else=NA")
final.data$age<-data$V081104
final.data$age[data$V081104<17]<-NA
final.data$income<-as.numeric(data$V083248)
final.data$income[as.numeric(data$V083248)<4]<-NA
final.data$income<-final.data$income-4
final.data$education<-recode(as.numeric(data$V083218x), "0:5=0; 6:7=1; else=NA") #College degree
#### Political Covariates ####
final.data$ideology<-zero.one(recode(as.numeric(data$V083069), "1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA")) #Conservative
final.data$ideology.post<-zero.one(recode(as.numeric(data$V085084), "1=1; 2=2; 3=3; 4=4; 5=5; 6=6; 7=7; else=NA")) #Conservative
final.data$id<-rowMeans(cbind(final.data$ideology, final.data$ideology.post), na.rm=T)
final.data$pid<-zero.one(recode(as.numeric(data$V083098x), "0=1; 1=2; 2=3; 3=4; 4=5; 5=6; 6=7; else=NA")) #Republican
final.data$pid.3<-recode(as.numeric(data$V083098x), "0=-1; 1=-1; 2=-1; 3=0; 4=1; 5=1; 6=1; else=NA") #Leaner as partisan
##Vote choice##
final.data$vote.obama<-recode(as.numeric(data$V083169a), "1=1; 2=0; else=NA")
final.data$vote.mccain.post<-recode(as.numeric(data$V085195), "1=0; 3=1; else=NA")

##Feeling thermometer##, post
final.data$feel.obama<-data$V083037a
final.data$feel.obama[final.data$feel.obama<0]<-NA
final.data$feel.obama<-zero.one(final.data$feel.obama)

##Feeling thermometer##, post
final.data$feel.mccain<-data$V083037b
final.data$feel.mccain[final.data$feel.mccain<0]<-NA
final.data$feel.mccain<-zero.one(final.data$feel.mccain)

##Feeling thermometer##, post
final.data$feel.dem<-data$V083044a
final.data$feel.dem[final.data$feel.dem<0]<-NA
final.data$feel.dem<-zero.one(final.data$feel.dem)

##Feeling thermometer##, post
final.data$feel.rep<-data$V083044b
final.data$feel.rep[final.data$feel.rep<0]<-NA
final.data$feel.rep<-zero.one(final.data$feel.rep)

final.data$affective.polarization<-final.data$feel.rep-final.data$feel.dem
final.data$affective.polarization2<-final.data$feel.mccain-final.data$feel.obama
final.data$pid.3<-as.factor(recode(final.data$pid.3, "-1='Democrat'; 0='Independent'; 1='Republican'"))
final.data$pid.3 <- relevel(final.data$pid.3, ref = "Republican")

#### Participation items #######, post
final.data$eff1a<-recode(as.numeric(data$V083079a), "1=1; 2=2; 3=3; 4=4; 5=5; else=NA") #TOO COMPLICATED
final.data$eff2a<-recode(as.numeric(data$V083079b), "1=5; 2=4; 3=3; 4=2; 5=1; else=NA") #Good understanding of political issues
final.data$eff3a<-recode(as.numeric(data$V083079c), "1=1; 2=2; 3=3; 4=4; 5=5; else=NA") #Public officials don't care what I think
final.data$eff4a<-recode(as.numeric(data$V083079d ), "1=1; 2=2; 3=3; 4=4; 5=5; else=NA") #Don't have a say in what gov does
psych::alpha(cbind(final.data$eff1a, final.data$eff2a, final.data$eff3a, final.data$eff4a)) ### Shitty reliability
###  Efficacy
final.data$eff1b<-recode(as.numeric(data$V083080a), "4=1; 5=2; 6=3; 7=4; 8=5; else=NA") #TOO COMPLICATED
final.data$eff2b<-recode(as.numeric(data$V083080b), "4=5; 5=4; 6=3; 7=2; 8=1; else=NA") #Good understanding of political issues
final.data$eff3b<-recode(as.numeric(data$V083080c), "4=5; 5=4; 6=3; 7=2; 8=1; else=NA") #Public officials don't care what I think
final.data$eff4b<-recode(as.numeric(data$V083080d ), "4=5; 5=4; 6=3; 7=2; 8=1; else=NA") #Don't have a say in what gov does
### Voted and Participation ###
final.data$voted<-recode(as.numeric(data$V085036x  ), "0=0; 1=1; else=NA") #Voted
final.data$p1<- recode(as.numeric(data$V085029), "1=1; 5=0; else=NA") # convince others
final.data$p2<- recode(as.numeric(data$V085030), "1=1; 5=0; else=NA") # rally
final.data$p3<- recode(as.numeric(data$V085031), "1=1; 5=0; else=NA") #  button
final.data$p4<- recode(as.numeric(data$V085032), "1=1; 5=0; else=NA") # work for party
final.data$p5<- recode(as.numeric(data$V085033), "1=1; 5=0; else=NA") # contribute money
final.data$p6<- recode(as.numeric(data$V085034), "1=1; 5=0; else=NA") # contribute money to party
save(final.data, file="/Users/chrisweber/desktop/Advances Analysis/Data/ANES.data.Rdata")


#### Extra Information ####
# GSS data

####-------------------------------------------------------------------------------------####
# File to construct a GSS Analysis file
####-------------------------------------------------------------------------------------####
rm(list=ls())
require(foreign)
require(car)
detach("package:dplyr")
#require(dplyr)

####-------------------------------------------------------------------------------------####
###  Data Description ##

#gss
gss<-read.dta("/Users/chrisweber/Desktop/Advances Analysis/GSS/data/GSS7214_R5.DTA", convert.factors=FALSE)
table(gss$year)
table(gss$id)
data<-data.frame(year=as.character(gss$year))
data$id<-gss$id
data$pid<-recode(gss$partyid, "7=NA")/6  #High score is Republican
data$ideo<-(gss$polviews-1)/6 # High score is conservative

##Save the data ###
save(data, file= "/Users/chrisweber/Desktop/Advances Analysis/data/gss.coded.Rdata")

